import 'package:enterprise_v6_app/app/components/special_operation/custom_web_count_down_page.dart';
import 'package:enterprise_v6_app/app/core/constans/weather_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_risk_analysis_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/management_operation_page/management_operation_controller.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

/// 管理作业
class ManagementOperationPage extends StatelessWidget {
  ManagementOperationPage({super.key});

  final controller = Get.put(ManagementOperationController());

  // 按钮显示
  Widget _buttonView(GetRiskAnalysisEntity bean) {
    final int i = controller.rightIcon.indexOf(bean);
    if(bean.logoIcon == null){
      return SvgPicture.asset(
        AppImages.icon_web_person,
        colorFilter: i == controller.selectedRightIndex
            ? null
            : const ColorFilter.mode(Colors.white, BlendMode.srcIn),
      );
    }
    return !bean.logoIcon!.endsWith('svg') ? Image.network(
      Interface.fileUrl + bean.logoIcon!,
      width: 26,
      height: 26,
    ) : SvgPicture.asset(
      bean.logoIcon!,
      colorFilter: i == controller.selectedRightIndex
          ? null
          : const ColorFilter.mode(Colors.white, BlendMode.srcIn),
    );
  }

  /// 右侧按钮
  /// [bean] 按钮数据
  Widget _rightButton(GetRiskAnalysisEntity bean) {
    final int i = controller.rightIcon.indexOf(bean);
    return Container(
      padding: const EdgeInsets.only(top: 4, bottom: 4, left: 4, right: 4),
      child: Column(
        children: [
          _buttonView(bean),
          //  !bean.logoIcon!.endsWith('svg') ? Image.network(
          //    Interface.fileUrl+ bean.logoIcon!,
          //    width: 26,
          //    height: 26,
          //  ) : SvgPicture.asset(
          //   bean.logoIcon!,
          //   colorFilter: i == controller.selectedRightIndex
          //       ? null
          //       : const ColorFilter.mode(Colors.white, BlendMode.srcIn),
          // ),
          const SizedBox(
            height: 2,
          ),
          Text(
            bean.name ??'空',
            style: TextStyle(
              fontSize: 11,
              color: i == controller.selectedRightIndex ? const Color(0xFF00E5FF) : Colors.white,
            ),
          )
        ],
      ).click(() {
        controller.clickRightIcon(i,bean);
      }),
    );
  }

  /// 左侧按钮
  Widget _leftButton(String icon,String title) {
    return SizedBox(
      height: 35,
      child: Row(
        children: [
          Container(
            height: 34,
            width: 34,
            padding: const EdgeInsets.all(6),
            decoration: BoxDecoration(
              color: const Color(0x8A122041),
              borderRadius: BorderRadius.circular(8),
            ),
            child: SvgPicture.asset(
              icon,
            ),
          ),
          const SizedBox(
            width: 5,
          ),
          Container(
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Color(0x0013DAFF),
                  Color(0x782A7BDF),
                  Color(0x0013DAFF),
                ]
              )
            ),
            child: Text(
              title,
              style:  const TextStyle(
                fontSize: 12,
                color: Color(0xFFBEF8FF),

            ),),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) => Scaffold(
        body: Stack(
          alignment: Alignment.center,
          children: [
            InAppWebView(
              initialUrlRequest: controller.url,
              onWebViewCreated: controller.onWebViewCreated,
            ),
            Positioned(
                bottom: 36,
                child: Row(
                  children: [
                    Stack(
                      alignment: Alignment.center,
                      children: [
                        SvgPicture.asset(
                          AppImages.icon_web_button_bg,
                        ),
                        Row(
                          children: [
                            SvgPicture.asset(
                              AppImages.icon_work_1,
                            ),
                            const SizedBox(width: 6),
                            const Text(
                            '作业中断',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 11,
                                fontWeight: FontWeight.w500,
                              ),
                            )
                          ],
                        ),
                      ],
                    ).click(controller.jobBreak),
                    const SizedBox(
                      width: 36,
                    ),
                    Stack(
                      alignment: Alignment.center,
                      children: [
                        SvgPicture.asset(
                          AppImages.icon_web_button_bg,
                        ),
                        Row(
                          children: [
                            SvgPicture.asset(
                              AppImages.icon_work_2,
                            ),
                            const SizedBox(width: 6),
                            const Text(
                              '终止作业',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 11,
                                fontWeight: FontWeight.w500,
                              ),
                            )
                          ],
                        ),
                      ],
                    ).click(controller.jobStop),
                    const SizedBox(
                      width: 36,
                    ),
                    Stack(
                      alignment: Alignment.center,
                      children: [
                        SvgPicture.asset(
                          AppImages.icon_web_button_bg,
                        ),
                        Row(
                          children: [
                            SvgPicture.asset(
                              AppImages.icon_work_3,
                            ),
                            const SizedBox(width: 6),
                            const Text(
                              '作业验收',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 11,
                                fontWeight: FontWeight.w500,
                              ),
                            )
                          ],
                        ),
                      ],
                    ).click(controller.jobAccept),
                  ],
                )),
            Positioned(
              left: 20,
              bottom: 36,
              child: SvgPicture.asset(
                AppImages.icon_web_back,
              ).click(controller.goBack),
            ),
            Positioned(
              bottom: 75,
              left: 20,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _leftButton(AppImages.icon_meeting,'会议').clickOnce(controller.mettingButton),
                  const SizedBox(
                    height: 8,
                  ),
                  _leftButton(AppImages.icon_handover, '交接班').clickOnce(controller.reliefOfAShift),
                  const SizedBox(
                    height: 8,
                  ),
                  _leftButton(AppImages.icon_violation, '违规登记').clickOnce(controller.illegalRegistration),
                  const SizedBox(
                    height: 8,
                  ),
                  _leftButton(AppImages.icon_live, '直播录像').clickOnce(controller.liveVideoRecording),
                ],
              ),
            ),
            Positioned(
              left: 20,
              top: 14,
              child: Row(
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
                    decoration: BoxDecoration(
                      color: const Color(0x80253053),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Row(
                      children: [
                        Text('${WeatherInfo.weatherLives?.temperature} ℃',
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 12,
                            )),
                        Container(
                          margin: const EdgeInsets.only(
                            left: 10,
                            right: 10,
                          ),
                          width: 2,
                          height: 10,
                          color: Colors.white,
                        ),
                        Text(
                          '${WeatherInfo.weatherLives?.weather}',
                          style: const TextStyle(
                            color: Colors.white,
                            fontSize: 12,
                          ),
                        )
                      ],
                    ),
                  ),
                  const SizedBox(
                    width: 12,
                  ),
                  // Container(
                  //   padding:
                  //       const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
                  //   decoration: BoxDecoration(
                  //     color: const Color(0xB2FFFFFF),
                  //     borderRadius: BorderRadius.circular(4),
                  //   ),
                  //   child: const Row(
                  //     children: [
                  //       Text('XXX气体报警',
                  //           style: TextStyle(
                  //             color: Color(0xFFFD4B4B),
                  //             fontSize: 12,
                  //           )),
                  //     ],
                  //   ),
                  // ),
                  const SizedBox(
                    width: 10,
                  ),
                  Visibility(
                    visible: controller.ticketReceiptValidDateEntity
                        .gasDetectionValidDate != 0,
                    child: CustomWebGasCountDownPage(
                      milliseconds: controller.ticketReceiptValidDateEntity.gasDetectionValidDate ?? 0,
                      title: '气体分析',
                      fontColor: const TextStyle(
                        fontSize: 12,
                        color: Color(0x80253053)
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 8,
                  ),
                  Visibility(
                    visible: controller.ticketReceiptValidDateEntity
                        .ticketReceiptValidDate != 0,
                    child:  CustomWebGasCountDownPage(
                      milliseconds: controller.ticketReceiptValidDateEntity
                          .ticketReceiptValidDate ?? 0,
                      title: '作业票',
                      fontColor: const TextStyle(
                          fontSize: 12,
                          color: Color(0x80253053)
                      ),
                    ),
                  )
                ],
              ),
            ),
            Positioned(
              top: 60,
              left: 20,
              child: Visibility(
                visible: controller.ticketPlanReceiptLists.liveStatus == 1,
                child: Container(
                  width: 97,
                  height: 52,
                  decoration: BoxDecoration(
                    color: Colors.black,
                    border: Border.all(
                      width: 1,
                      color: Colors.white,
                    )
                  ),
                  child: const Icon(Icons.open_in_full,color: Colors.white),
                ).click(controller.toLiveView),
              ),
            ),
            Positioned(
              right: 0,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    AppImages.icon_web_right_bg,
                    height: MediaQuery.of(context).size.height,
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height,
                    width: 60,
                    child: SingleChildScrollView(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: controller.rightIcon.map(_rightButton).toList(),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Positioned(
              right: 62,
              bottom: 12,
              child: Visibility(
                visible: controller.isShowWorkingPersonnelList,
                child: Container(
                  width: 170,
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: const Color(0xB2253053),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          const Text(
                            '当前作业人数：',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 12,
                            ),
                          ),
                          Text('${controller.currentWorkingPersonnel}人',
                              style: const TextStyle(
                                color: Color(0xFF00F3FF),
                                fontSize: 12,
                              )),
                        ],
                      ),
                      const SizedBox(
                        height: 8,
                      ),
                      Row(
                        children: [
                          const Text('作业人：',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 12,
                              )),
                          Expanded(
                            child: Text(controller.workingPersonnel.join(','),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 12,
                                )),
                          ),
                        ],
                      ),
                      const SizedBox(
                        height: 8,
                      ),
                      Row(
                        children: [
                          const Text('作业负责人：',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 12,
                              )),
                          Expanded(
                            child: Text(controller.responsiblePersonnel.join(','),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 12,
                                )),
                          ),
                        ],
                      ),
                      const SizedBox(
                        height: 8,
                      ),
                      Row(
                        children: [
                          const Text('监护人：',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 12,
                              )),
                          Expanded(
                            child: Text(controller.monitoringPersonnel.join(','),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 12,
                                )),
                          ),
                        ],
                      ),
                      const SizedBox(
                        height: 8,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          const Text('更多',style: TextStyle(
                            color: Color(0xFF00F3FF),
                            fontSize: 12,
                          )),
                          Image.asset(
                            AppImages.icon_web_more,
                            width: 15,
                            height: 15,
                          )
                        ],
                      ).click(controller.moreWork)
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
